<template>
  <a-card>
    <a-tabs type="card" v-model="standardType">
      <a-tab-pane key="1" tab="综合指标">
      </a-tab-pane>
      <a-tab-pane key="2" tab="技术指标">
      </a-tab-pane>
    </a-tabs>

    <a-spin :spinning="confirmLoading">
      <a-card :bordered="false" class="card">
        <a-row type="flex" style="margin-top: 10px">
          <a-col :span="10">
            <a-card :bordered="false" :body-style="{ padding: '0px' }">
              <a-table
                ref="table"
                size="middle"
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="false"
                :expandedRowKeys="expandedRowKeys"
                @expand="handleExpand"
                :defaultExpandAllRows="true"
                :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
              >
              </a-table>
            </a-card>
          </a-col>
          <a-col :span="14">
            <a-card :bordered="false" :body-style="{ padding: '0px' }">
              <a-row>
                <a-button style="float: right" type="primary" icon="save" @click="handleSave">保存</a-button>
              </a-row>
              <a-row style="margin-top: 10px; height: 800px">
                <a-spin :spinning="confirmLoading">
                  <j-form-container>
                    <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
                      <a-row>
                        <a-col :span="24">
                          <a-form-model-item
                            label="指标名称"
                            :labelCol="labelCol"
                            :wrapperCol="wrapperCol"
                            prop="indicatorName"
                          >{{selectionRows.indicatorName}}
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item
                            label="指标类型"
                            :labelCol="labelCol"
                            :wrapperCol="wrapperCol"
                            prop="indicatorType"
                          >
                            {{selectionRows.indicatorType}}
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item
                            label="最小分值"
                            :labelCol="labelCol"
                            :wrapperCol="wrapperCol"
                            prop="scoreMin"
                          >{{selectionRows.scoreMin}}
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item
                            label="最大分值"
                            :labelCol="labelCol"
                            :wrapperCol="wrapperCol"
                            prop="scoreMax"
                          >{{selectionRows.scoreMax}}
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item label="权重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
                            {{selectionRows.weight}}
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item label="分值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="score">
                            <a-input-number v-model="model.score" placeholder="请输入分值" style="width: 100%" />
                          </a-form-model-item>
                        </a-col>
                        <a-col :span="24">
                          <a-form-model-item
                            label="评审说明"
                            :labelCol="labelCol"
                            :wrapperCol="wrapperCol"
                            prop="description"
                          >
                            <a-textarea v-model="model.description" rows="4" placeholder="请输入评审说明" />
                          </a-form-model-item>
                        </a-col>
                      </a-row>
                    </a-form-model>
                  </j-form-container>
                </a-spin>
              </a-row>
            </a-card>
          </a-col>
        </a-row>
      </a-card>

      <!-- <footer-tool-bar>
        <a-button type="primary" @click="handleSubmit" :loading="loading">提交</a-button>
        <a-button style="margin-left: 8px" @click="handleSave">保存</a-button>
      </footer-tool-bar> -->
    </a-spin>
  </a-card>

</template>

<script>
import FooterToolBar from '@/components/tools/FooterToolBar'
import '@/assets/less/TableExpand.less'
import ChapterList from '@/views/exZbwj/ScoreChapter/ChapterListMini'

import { httpAction, getAction } from '@/api/manage'

export default {
  name: 'exTenderFileAdd',
  components: { ChapterList, FooterToolBar },
  props: {
    entryId: {
      default: ''
    },
    scoreStandardId:{

    }
  },
  data () {
    return {
      url: {
        getChildListBatch: '/exTenderFile/scoreChapter/getChildListBatch',
        list: '/exTenderFile/scoreChapter/rootList'
      },
      selectionRows:{},
      //指标类型（评分指标、价格指标,
      standardType: '综合指标',
      /* 数据源 */
      dataSource: [],
      // 表头
      columns: [
        {
          title: ' ',
          align: 'left',
          dataIndex: 'name',
        },
        {
          title: ' ',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      expandedRowKeys: [],

      /* table选中keys*/
      selectedRowKeys: [],
      // ---- end,
      disableSubmit: false,
      visible: false,
      varName: null,
      noticeContent: '',
      confirmLoading: false,
      scoreStandardInfo: { projectId: '', id: '' },
      model: {},
      validatorRules: {
        indicatorName: [{ required: true, message: '请输入指标名称!' }],
        indicatorType: [{ required: true, message: '请选择指标类型!' }]
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 10 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 12 }
      }
    }
  },
  watch: {
  },
  created () {
  },
  mounted () {
    //this.$refs.realForm.addBefore()
    this.loadData()
  },
  methods: {

    loadData (arg) {
      this.loading = true
      let params = {
        pageNo: 1,
        pageSize: 10,
        scoreStandardId: '1852350211775614977',
        standardType: '综合指标'
      }
      // params.scoreStandardId = this.scoreStandardId
      // params.standardType = this.standardType
      return new Promise((resolve) => {
        getAction(this.url.list, params)
          .then((res) => {
            if (res.success) {
              let result = res.result
              if (Number(result.total) > 0) {
                this.dataSource = this.getDataByResult(res.result.records)
                this.expandedRowKeys = res.result.records.map((e) => e.id)
                //update--begin--autor:lvdandan-----date:20201204------for：JT-31 删除成功后默认展开已展开信息
                return this.loadDataByExpandedRows(this.dataSource)
                //update--end--autor:lvdandan-----date:20201204------for：JT-31 删除成功后默认展开已展开信息
              } else {
                this.dataSource = []
              }
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
      })
    },
    getDataByResult(result) {
      if (result && result.length > 0) {
        return result.map((item) => {
          //判断是否标记了带有子节点
          if (item[this.hasChildrenField] == '1') {
            let loadChild = { id: item.id + '_loadChild', name: 'loading...', isLoading: true }
            item.children = [loadChild]
          }
          return item
        })
      }
    },
    handleCancel () {
      this.close()
    },
    loadScoreStandardInfo () {
      var that = this
      getAction('exScoreStandard/exScoreStandard/queryById?id=' + that.scoreStandardId)
        .then((res) => {
          if (res.success) {
            that.scoreStandardInfo = res.result
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    handleSubmit () {
      this.handleSave()
    },
    handleSave () {
      var that = this
      if (that.$refs.chapterRef.selectedRowKeys.length == 0) {
        that.$message.warning('请选择分类')
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          var cid = that.$refs.chapterRef.selectedRowKeys[0]
          var url = 'exTenderFile/exZbwjScoreStandardDetail/add'
          let param = Object.assign({}, that.model)
          param.categoryId = cid
          if (that.model && that.model.id) {
            param.id = that.model.id
            url = 'exTenderFile/exZbwjScoreStandardDetail/edit'
          }
          param.standardType = this.standardType
          httpAction(url, param, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    chapterChange (selectVal) {
      var that = this
      that.confirmLoading = true
      getAction('exTenderFile/exZbwjScoreStandardDetail/list', { categoryId: selectVal })
        .then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              that.model = res.result.records[0]
            } else {
              that.model = {}
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    exTenderFileInfoEdit () {
      this.$refs.refSetVariableList.show()
    },
    handleExpand(expanded, record) {
      // 判断是否是展开状态
      // if (expanded) {
      //   this.expandedRowKeys.push(record.id)
      //   if (record.children.length > 0 && record.children[0].isLoading === true) {
      //     let params = this.getQueryParams() //查询条件
      //     params[this.pidField] = record.id
      //     getAction(this.url.childList, params).then((res) => {
      //       if (res.success) {
      //         if (res.result && res.result.length > 0) {
      //           record.children = this.getDataByResult(res.result)
      //           this.dataSource = [...this.dataSource]
      //         } else {
      //           record.children = ''
      //           record.hasChildrenField = '0'
      //         }
      //       } else {
      //         this.$message.warning(res.message)
      //       }
      //     })
      //   }
      // } else {
      //   let keyIndex = this.expandedRowKeys.indexOf(record.id)
      //   if (keyIndex >= 0) {
      //     // this.expandedRowKeys.splice(keyIndex, 1)
      //   }
      // },
    },
    // 根据已展开的行查询数据（用于保存后刷新时异步加载子级的数据）
    loadDataByExpandedRows(dataList) {
      if (this.expandedRowKeys.length > 0) {
        return getAction(this.url.getChildListBatch, { parentIds: '1852350256293957634' }).then((res) => {
          if (res.success && res.result.records.length > 0) {
            //已展开的数据批量子节点
            let records = res.result.records
            const listMap = new Map()
            for (let item of records) {
              let pid = item[this.pidField]
              if (this.expandedRowKeys.join(',').includes(pid)) {
                let mapList = listMap.get(pid)
                if (mapList == null) {
                  mapList = []
                }
                mapList.push(item)
                listMap.set(pid, mapList)
              }
            }
            let childrenMap = listMap
            let fn = (list) => {
              if (list) {
                list.forEach((data) => {
                  if (this.expandedRowKeys.includes(data.id)) {
                    data.children = this.getDataByResult(childrenMap.get(data.id))
                    fn(data.children)
                  }
                })
              }
            }
            fn(dataList)
          }
        })
      } else {
        return Promise.resolve()
      }
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      console.log(selectionRows)
    },
    // 添加子分类时获取所有父级id
    getExpandKeysByPid(pid, arr, all) {
      if (pid && arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == pid) {
            this.subExpandedKeys.push(arr[i].id)
            this.getExpandKeysByPid(arr[i]['pid'], all, all)
          } else {
            this.getExpandKeysByPid(pid, arr[i].children, all)
          }
        }
      }
    },

  }
}
</script>

<style scoped>
</style>
